package entity;

import loading.ImageLoader;
import math.Vector2D;

import java.awt.geom.*; 
import java.awt.*;

/**
 * @author Greg G
 * 
 * The entity class is the superclass for all objects in the game
 * Any static entity will directly extend this, and dynamic entities
 * will extend DynamicEntity, a subclass of Entity. Entity has methods
 * and variables for positioning and rotating objects.
 */
public abstract class Entity 
{
	private ImageLoader image;
	
	private Vector2D position;
	
	private double angle;
	private double radius;
	
	public Entity( Vector2D pos, ImageLoader img )
	{ 
		position = pos;
		image = img;
		radius = img.getImage().getWidth() / 2.0;
		angle = 0;
	}
	
	public abstract void drawInformation( Graphics2D g );

	public Rectangle2D.Double getBounds()
	{
		return new Rectangle2D.Double( position.getX() - image.width()/2,
									   position.getY() - image.height()/2,
									   image.width(),
									   image.height() );
	}
	
	public void update( double duration )
	{
		//first, fix it between 0 and 2PI
		if(angle > 2*Math.PI)
			angle = angle % (2*Math.PI);
		if(angle < 0)
			angle = 2*Math.PI - angle % (2*Math.PI);
	}
	
	public void setAngle( double ang )
	{
		angle = ang;
	}
	
	public double getAngle()
	{
		return angle;
	}
	
	public void setPosition( double x, double y )
	{
		position.setX( x );
		position.setY( y );
	}
	
	public void setPosition( Vector2D v )
	{
		position = v;
	}
	
	public Vector2D getPosition()
	{
		return position;
	}
	
	public void setRadius( double rad )
	{
		radius = rad;
	}
	
	public double getRadius()
	{
		return radius;
	}
	
	protected ImageLoader getImage()
	{
		return image;
	}
	
	//draw the entity to the graphics object
	public void draw( Graphics2D g )
	{
		AffineTransform at = new AffineTransform();
		//at.rotate( angle );
		at.rotate(angle, position.getX(), position.getY());
		at.translate(position.getX() - image.width()/2, position.getY() - image.height()/2);
		//draw the image based on the rotation and loaded image
		image.draw(g, at);
	}
}


